package controler;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.SanphamModel;
import entity.Sanpham;

import controler.search;
import dao.common;

/**
 * Servlet implementation class admintimsanpham
 */
@WebServlet("/admintimsanpham")
public class admintimsanpham extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public admintimsanpham() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ServletException{  	
    	response.setContentType("text/html;charset=UTF-8");
    	HttpSession session = request.getSession();
    	common cm = new common();   	
    	if(cm.checkquyen(session)== false){
    		response.sendRedirect("listDanhmuc");
    	}
    	else{
	    	search src = new search();
	        PrintWriter out = response.getWriter();
	        
	        String ten = request.getParameter("name");
	        if(ten != null){
	        	ten = new String(ten.getBytes("8859_1"),"UTF8");
	        }
	        String thuonghieu = request.getParameter("thuongthieu");
	        if(thuonghieu != null){
	        	thuonghieu = new String(thuonghieu.getBytes("8859_1"),"UTF8");
	        }
	        String madanhmuc = request.getParameter("MADANHMUC");
	        String pricemax = request.getParameter("pricemax");
	        String pricemin = request.getParameter("pricemin");
	        
	        String strTong = src.chuoitimtongdong(ten,madanhmuc,thuonghieu,pricemax,pricemin);
	        
	        try {
	            request.setCharacterEncoding("UTF-8");
	            int page = 1;
	            int records = 4;
	
	            if (request.getParameter("page") != null) {
	                page = Integer.parseInt(request.getParameter("page"));
	            }
	            List<Sanpham> list = new ArrayList<Sanpham>();
	            SanphamModel model = new SanphamModel();
	
	            int start_row = (page * records) - (records - 1);
	            int end_row = start_row + records - 1;
	            
	            String strSearch = src.chuoitimkiem(ten,madanhmuc,thuonghieu,pricemax,pricemin,start_row,end_row);
	
	            list = model.timsanpham(strSearch);
	            int noOfRecords = model.getTotalRows(strTong);
	            //int noOfPage = ;
	            int noOfPage = ((int) Math.ceil(noOfRecords * 1.0 / records));
	
	            request.setAttribute("list", list);
	            request.setAttribute("start", start_row);
	            request.setAttribute("end", end_row);
	            request.setAttribute("noOfPages", noOfPage);
	            request.setAttribute("currentPage", page);
	            request.setAttribute("keyword", ten);
	
	            RequestDispatcher view = request.getRequestDispatcher("adminxemsanpham.jsp");
	            view.forward(request, response);
	        } finally {
	            out.close();
	        }
    	}
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
